package com.zhangyu.shadow.repair.delegate.irregular;

import com.alibaba.fastjson.TypeReference;
import com.zhangyu.shadow.repair.base.IStringDataConvert;
import com.zhangyu.shadow.repair.config.RequestUrls;
import com.zhangyu.shadow.repair.delegate.bean.VehicleBean;
import com.zhangyu.shadow.repair.delegate.search.MaintainDataConverter;
import com.zhangyu.shadow.repair.delegate.search.entity.MaintainEntity;
import com.zhangyu.shadow.repair.intercept.DataIntercept;
import com.zhangyu.shadow.repair.manager.AccountManager;

import java.util.ArrayList;

import io.reactivex.disposables.Disposable;
import st.lib.base.net.callback.IError;
import st.lib.base.net.callback.ISuccess;
import st.lib.base.net.rx.RxRestClient;
import st.lib.base.ui.recycler.MultipleItemEntity;
import st.lib.base.ui.recycler.ObjConverter;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/7
 */
public class IrregularBusModel implements IrregularBusContract.Model {
    @Override
    public Disposable searchBus(String code, final IStringDataConvert<ArrayList<MultipleItemEntity>> convert, IError error) {
        return RxRestClient.builder()
                .url(RequestUrls.URL_MAINTAIN_SEARCH_VEHICLE)
                .params("vehicleCode", code)
                .params(AccountManager.getInstance().getBaseInfo())
                .interceptor(new DataIntercept())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        if (response != null) {
                            final MaintainDataConverter converter = new MaintainDataConverter();
                            converter.setJsonData(response);
                            convert.convert(converter.convert());
                        } else {
                            convert.convert(new ArrayList<MultipleItemEntity>());
                        }
                    }
                })
                .error(error)
                .build()
                .post();
    }

    @Override
    public Disposable getHistoryList(String url,
                                     ArrayList<String> busVmTypeName,
                                     ArrayList<Integer> classificationId,
                                     String depId,
                                     String code,
                                     String dateStart,
                                     String dateEnd,
                                     int busVmType,
                                     final IStringDataConvert<ArrayList<VehicleBean>> convert,
                                     IError error) {
        return RxRestClient.builder()
                .url(url)
                .params(AccountManager.getInstance().getBaseInfo())
                .params("busVmTypeName", busVmTypeName)
                .params("classificationId", classificationId)
                .params("searchStart", dateStart)
                .params("searchEnd", dateEnd)
                .params("vehicleCode", code)
                .params("deptId", depId)
                .params("busVmType", busVmType)
                .interceptor(new DataIntercept())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        convert.convert(convertBusListBean(response));
                    }
                })
                .error(error)
                .build()
                .post();
    }

    @Override
    public Disposable getVehicleList(String url,
                                     String depId,
                                     String code,
                                     String dateStart,
                                     String dateEnd,
                                     int busVmType,
                                     String minDate,
                                     int id,
                                     final IStringDataConvert<ArrayList<VehicleBean>> convert,
                                     IError error) {
        return RxRestClient.builder()
                .url(url)
                .params(AccountManager.getInstance().getBaseInfo())
                .params("searchStart", dateStart)
                .params("searchEnd", dateEnd)
                .params("vehicleCode", code)
                .params("minTime", minDate)
                .params("deptId", depId)
                .params("minId", id)
                .params("busVmType", busVmType)
                .interceptor(new DataIntercept())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        convert.convert(convertBusListBean(response));
                    }
                })
                .error(error)
                .build()
                .post();
    }

    private ArrayList<VehicleBean> convertBusListBean(String response) {
        return new ObjConverter<ArrayList<VehicleBean>>().convert(response
                , new TypeReference<ArrayList<VehicleBean>>() {
                });
    }

    private MaintainEntity convertSearchMaintainBean(String response) {
        return new ObjConverter<MaintainEntity>().convert(response,
                new TypeReference<MaintainEntity>() {
                });
    }
}
